<template>
  <div>
    <el-dialog
      :title="obj.vAdd ? '商品添加' : '商品编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="empty()"
      @opened="opened()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="一级分类">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="changeCate()"
          >
            <el-option
              v-for="(item, index) in acte"
              :key="index"
              :value="item.id"
              :label="item.catename"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="form.second_cateid" placeholder="请选择二级分类">
            <el-option
              v-for="(item, index) in secondCate"
              :key="index"
              :value="item.id"
              :label="item.catename"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="form.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格">
          <el-input v-model="form.market_price"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <div class="fileBox1">
            <h2>+</h2>
            <input v-if="obj.isTrue" type="file" @change="changeImg1" />
            <img :src="imgUrl" alt="" v-if="imgUrl" />
          </div>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-select
            v-model="form.specsid"
            placeholder="请选择商品规格"
            @change="changeSpecs()"
          >
            <el-option
              v-for="(item, index) in specs"
              :key="index"
              :value="item.id"
              :label="item.specsname"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性">
          <el-select
            multiple
            v-model="form.specsattr"
            placeholder="请选择规格属性"
          >
            <el-option
              v-for="(item, index) in attrArr"
              :key="index"
              :value="item"
              :label="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品">
          <el-radio v-model="form.isnew" :label="1">是</el-radio>
          <el-radio v-model="form.isnew" :label="2">否</el-radio>
        </el-form-item>
        <el-form-item label="是否热卖">
          <el-radio v-model="form.ishot" :label="1">是</el-radio>
          <el-radio v-model="form.ishot" :label="2">否</el-radio>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-form-item label="商品描述">
          <div id="div1" v-if="obj.isTrue"></div>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" v-if="obj.vAdd" @click="add()"
          >确 定</el-button
        >
        <el-button type="primary" v-else @click="update()">编辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor";
import { mapActions, mapGetters } from "vuex";
import {
  goodsadd,
  catelist,
  goodsinfo,
  goodsedit,
} from "../../../request/api.js";
import { successAlert, warningAlert } from "../../../utils/alert.js";
export default {
  props: ["obj"],
  data() {
    return {
      imgUrl: "",
      form: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: [],
        isnew: 1,
        ishot: 1,
        status: 1,
      },
      // 用来渲染二级分类的
      secondCate: [],
      // 用来渲染规格属性的
      attrArr: [],
    };
  },
  computed: {
    ...mapGetters({
      acte: "cate/list",
      specs: "specs/list",
    }),
  },
  methods: {
    ...mapActions({
      catelist: "cate/asyncChangeList",
      specslist: "specs/asyncChangeList",
      goodslist: "goods/asyncChangeList",
      totallist: "goods/asyncChangeTotal",
    }),
    //回显图片
    changeImg1(e) {
      let file = e.target.files[0];
      this.imgUrl = URL.createObjectURL(file);
      this.form.img = file;
    },
    //清空并关闭
    empty() {
      (this.form = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: [],
        isnew: 1,
        ishot: 1,
        status: 1,
      }),
        (this.imgUrl = "");
      this.secondCate = [];
      this.attrArr = [];
      this.obj.isTrue = false;
    },
    //当一级分类发生变化
    changeCate() {
      catelist({ pid: this.form.first_cateid }).then((res) => {
        // console.log(res);
        this.form.second_cateid = "";
        this.secondCate = res.data.list;
      });
    },
    //规格发生变化
    changeSpecs() {
      let obj = this.specs.find((item) => {
        return item.id == this.form.specsid;
      });
      this.form.specsattr = [];
      this.attrArr = obj.attrs;
    },
    //取消
    cancel() {
      this.empty();
    },
    //添加
    add() {
      let obj = {
        ...this.form,
        specsattr: JSON.stringify(this.form.specsattr),
        description: this.editor.txt.html(),
      };
      console.log(this.form);
      goodsadd(obj).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.empty();
          this.totallist();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    //获取一条信息
    getOne(id) {
      goodsinfo({ id: id }).then((res) => {
        this.form = {
          ...res.data.list,
          specsattr: JSON.parse(res.data.list.specsattr),
        };
        catelist({ pid: this.form.first_cateid }).then((res) => {
          this.secondCate = res.data.list;
        });
        this.imgUrl = this.$pre + this.form.img;
        let obj = this.specs.find((item) => {
          return item.id == this.form.specsid;
        });
        this.attrArr = obj.attrs;
        this.form.id = id;
      });
    },
    //确认编辑
    update() {
      let obj = {
        ...this.form,
        specsattr: JSON.stringify(this.form.specsattr),
      };
      goodsedit(obj).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.goodslist();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    opened() {
      this.editor = new E("#div1");
      this.editor.create();
      this.editor.txt.html(this.form.description);
    },
  },
  mounted() {
    this.catelist();
    this.specslist(true);
  },
};
</script>

<style scoped>
.fileBox1 {
  width: 200px;
  height: 200px;
  border: 1px dashed #ccc;
  position: relative;
}
.fileBox1 h2 {
  text-align: center;
  line-height: 200px;
  font-size: 60px;
  margin: 0;
}
.fileBox1 input {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  z-index: 3;
}
.fileBox1 img {
  width: 200px;
  height: 200px;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
}
</style>